﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PlatForm_Register : System.Web.UI.Page
{
	private PX.User.UserComponent clsUserComp = new PX.User.UserComponent();
	private PX.CommonControl clsCommonControl = new PX.CommonControl();
	private PX.Config clsConfig = new PX.Config();
	private PX.Asker.Component clsAskerComp = new PX.Asker.Component();
	private PX.UploadFile.UploadFileComponent clsUploadFileComp = new PX.UploadFile.UploadFileComponent();
	private PX.Expert.ExpertComponent clsExpertComp = new PX.Expert.ExpertComponent();

	protected void Page_Load(object sender, EventArgs e)
	{
		Master.MasterLabTitle.Text = "用户注册";

		if (!IsPostBack)
		{
			BindDepartmentDropDownList(DdlUserDepartment);
			BindDepartmentDropDownList(DdlExpertDepartment);
			BindExpertTypeDropDownList(DdlExpertType);
		}
	}

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		LBtnUserRegister.Click += new EventHandler(LBtnUserRegister_Click);
		LBtnExpertRegister.Click += new EventHandler(LBtnExpertRegister_Click);

		clsUserComp.page = Page;
		clsConfig.clsPage = Page;
		clsAskerComp.page = Page;
		clsUploadFileComp.page = Page;
		clsExpertComp.page = Page;
	}

	/// <summary>
	/// 专家注册按钮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnExpertRegister_Click(object sender, EventArgs e)
	{
		PX.UploadFile.UploadFile clsUploadFileImage = new PX.UploadFile.UploadFile();
		PX.UploadFile.UploadFile clsUploadFileIntroduction = new PX.UploadFile.UploadFile();
		PX.Expert.Expert clsAddExpert = new PX.Expert.Expert();

		if (FUploadExpertImage.HasFile)
		{
			if (clsUploadFileComp.Add(FUploadExpertImage, clsUploadFileImage))
			{
				clsUploadFileImage = clsUploadFileComp.GetOne(clsUploadFileComp.GetNewestAddID());
			}
		}

		if (FUploadIntroduction.HasFile)
		{
			if (clsUploadFileComp.Add(FUploadIntroduction, clsUploadFileIntroduction))
			{
				clsUploadFileIntroduction = clsUploadFileComp.GetOne(clsUploadFileComp.GetNewestAddID());
			}
		}

		clsAddExpert.Username = TbExpertUsername.Text;
		clsAddExpert.Password = TbExpertPassword.Text;
		clsAddExpert.Name = TbExpertName.Text;
		clsAddExpert.ImageID = clsUploadFileImage.ID;
		clsAddExpert.IntroductionID = clsUploadFileIntroduction.ID;
		clsAddExpert.DepartmentID = Convert.ToInt32(DdlExpertDepartment.Text);
		clsAddExpert.Type = Convert.ToInt32(DdlExpertType.Text);
		clsAddExpert.Job = TBJob.Text;

		if (clsExpertComp.AddExpert(clsAddExpert))
		{
			Common.ShowMessageAlert.ShowMessage(Page, "注册成功！");
		}
	}

	/// <summary>
	/// 提问者用户注册按钮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnUserRegister_Click(object sender, EventArgs e)
	{
		PX.User.User clsAddUser = new PX.User.User();
		PX.Asker.Entity clsAddAsker = new PX.Asker.Entity();

		clsAddAsker.Username = TbUserUsername.Text;
		clsAddAsker.Password = TbUserPassword.Text;
		clsAddAsker.Name = TbUserName.Text;
		clsAddAsker.DepartmentID = Convert.ToInt32(DdlUserDepartment.Text);
		clsAddAsker.Email = TbUserEMail.Text;
		clsAddAsker.Phone = TbUserPhone.Text;
		clsAddAsker.IsAnoynmous = false;

		if (clsAskerComp.Add(clsAddAsker))
		{
			Common.ShowMessageAlert.ShowMessage(Page, "注册成功！");
		}

		//if (clsUserComp.HaveSameUsername(TbUserUsername.Text))
		//{
		//        Common.ShowMessageAlert.ShowMessage(Page, "帐号已经存在！");
		//}
		//else
		//{
		//        clsAddUser.Username = TbUserUsername.Text;
		//        clsAddUser.Password = TbUserPassword.Text;
		//        clsAddUser.Name = TbUserName.Text;
		//        clsAddUser.DepartmentID = Convert.ToInt32(DdlUserDepartment.Text);
		//        clsAddUser.UserGroupID = 37;

		//        if (clsUserComp.Add(clsAddUser))
		//        {
		//                clsAddUser = clsUserComp.GetOne(clsUserComp.GetNewestAddID());

		//                clsAddAsker.UserID = clsAddUser.ID;
		//                clsAddAsker.DepartmentID = clsAddUser.DepartmentID;
		//                clsAddAsker.Email = TbUserEMail.Text;
		//                clsAddAsker.Phone = TbUserPhone.Text;
		//                clsAddAsker.IsAnoynmous = false;

		//                if (clsAskerComp.Add(clsAddAsker))
		//                {
		//                        Common.ShowMessageAlert.ShowMessage(Page, "注册成功！");
		//                }
		//        }
		//}
	}

	/// <summary>
	/// 绑定单位下拉列表数据
	/// </summary>
	private void BindDepartmentDropDownList(DropDownList ddl)
	{
		
		clsCommonControl.Department(ddl);

	}

	/// <summary>
	/// 绑定专家类型下拉类表
	/// </summary>
	/// <param name="ddl"></param>
	private void BindExpertTypeDropDownList(DropDownList ddl)
	{
		
		clsCommonControl.ExpertType(ddl);
	}
}
